package middleware

import (
	"time"

	"mcode/pkg/logger"

	"github.com/gin-gonic/gin"
)

// Logger 返回一个使用 zap 的日志中间件
func Logger() gin.HandlerFunc {
	return func(c *gin.Context) {
		start := time.Now()
		path := c.Request.URL.Path
		method := c.Request.Method

		// 处理请求
		c.Next()

		// 记录请求日志
		latency := time.Since(start)
		status := c.Writer.Status()
		reqID := c.GetHeader("X-Request-ID")

		logger.Info("HTTP Request",
			logger.String("request_id", reqID),
			logger.String("method", method),
			logger.String("path", path),
			logger.Int("status", status),
			logger.Duration("latency", latency),
			logger.String("ip", c.ClientIP()),
			logger.String("user_agent", c.GetHeader("User-Agent")),
		)
	}
}
